package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbBrand;
import com.bwie.service.TbBrandService;
import com.bwie.mapper.TbBrandMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author andy
* @description 针对表【tb_brand(品牌表)】的数据库操作Service实现
* @createDate 2025-06-20 15:27:42
*/
@Service
public class TbBrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand>
    implements TbBrandService{

    @Autowired
    TbBrandMapper tbBrandMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public R getBrandList(PageInfoVo pageInfoVo) {

        //--1 构造分页参数
        Page<TbBrand> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        //--2 分页查询
        QueryWrapper<TbBrand> wrapper = new QueryWrapper<>();

        // 品牌名称模糊查询
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())) {
            wrapper.lambda().like(TbBrand::getBrandName, pageInfoVo.getKeyWord());
        }

        wrapper.lambda().orderByDesc(TbBrand::getCreateTime);

        Page<TbBrand> tbBrandPage = tbBrandMapper.selectPage(page, wrapper);

        return R.ok().setData(tbBrandPage);
    }

    @Override
    public R addBrand(TbBrand tbBrand) {
        //--1 判断名称是否唯一
        TbBrand brand = tbBrandMapper.selectOne(
                new QueryWrapper<TbBrand>().lambda().eq(TbBrand::getBrandName, tbBrand.getBrandName())
        );
        if(brand != null) {
            return R.error().setMsg("品牌名称已存在");
        }

        // --2 设置审计字段
        String token = request.getHeader("token");
        if(token == null) {
            return R.error().setMsg("请先登录");
        }
        String userName = TokenUtils.getUserName(token);
        tbBrand.setCreateBy(userName);
        tbBrand.setCreateTime(new Date());

        //--3 添加
        tbBrandMapper.insert(tbBrand);

        return R.ok();
    }

    @Override
    public R changeBrandStatus(TbBrand tbBrand) {
        //--1 查询品牌信息
        TbBrand brand = tbBrandMapper.selectById(tbBrand.getBrandId());
        if(brand == null) {
            return R.error().setMsg("品牌不存在");
        }

        //--2 修改状态

        String token = request.getHeader("token");
        if(token == null) {
            return R.error().setMsg("请先登录");
        }
        String userName = TokenUtils.getUserName(token);

        brand.setUpdateBy(userName);
        brand.setUpdateTime(new Date());

        brand.setBrandStatus(tbBrand.getBrandStatus());
        tbBrandMapper.updateById(brand);


        return R.ok();
    }

    @Override
    public R deleteBrand(Integer brandId) {
        //--1 判断是否存在
        TbBrand tbBrand = tbBrandMapper.selectById(brandId);
        if(tbBrand == null) {
            return R.error().setMsg("品牌不存在");
        }

        //--2 删除
        tbBrandMapper.deleteById(brandId);
        return R.ok();
    }

    @Override
    public R deleteBatchBrand(List<TbBrand> brandList) {

//        for (TbBrand tbBrand : brandList) {
//            tbBrandMapper.deleteById(tbBrand.getBrandId());
//        }

        List<Integer> ids = brandList.stream().map(item -> item.getBrandId()).collect(Collectors.toList());
        tbBrandMapper.deleteBatchIds(ids);

        return R.ok();
    }
}




